package com.ruoyi.qualityControl.service.impl;

import com.github.pagehelper.PageHelper;
import com.ruoyi.qualityControl.domain.*;
import com.ruoyi.qualityControl.domain.vo.*;
import com.ruoyi.qualityControl.mapper.*;
import com.ruoyi.qualityControl.service.QcTimeExamineService;
import com.ruoyi.qualityControl.utils.GetDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class QcTimeExamineServiceImpl implements QcTimeExamineService {
    @Autowired
    private QcTimeExamineReceiveSheetMapper qcTimeExamineReceiveSheetMapper;
    @Autowired
    private QcTimeExamineReceiveCollectMapper qcTimeExamineReceiveCollectMapper;
    @Autowired
    private QcTimeExamineStrandedSheetMapper qcTimeExamineStrandedSheetMapper;
    @Autowired
    private QcTimeExamineStrandedCollectMapper qcTimeExamineStrandedCollectMapper;
    @Autowired
    private QcTimeExamineReturnGoodSheetMapper qcTimeExamineReturnGoodSheetMapper;
    @Autowired
    private QcTimeExamineReturnGoodCollectMapper qcTimeExamineReturnGoodCollectMapper;
    @Autowired
    private QcTimeExamineSignSheetMapper qcTimeExamineSignSheetMapper;
    @Autowired
    private QcTimeExamineSignCollectMapper qcTimeExamineSignCollectMapper;
    @Autowired
    private QcTimeExamineReceiveSevenMapper qcTimeExamineReceiveSevenMapper;
    @Autowired
    private QcTimeExamineReturnGoodSevenMapper qcTimeExamineReturnGoodSevenMapper;
    @Autowired
    private QcTimeExamineSignSevenMapper qcTimeExamineSignSevenMapper;
    @Autowired
    private QcTimeExamineStrandedSevenMapper qcTimeExamineStrandedSevenMapper;

    private List<String> sevenDate = GetDate.getSevenDate();

    @Override
    public List<QcTimeExamineReceiveSheet> receiveSheet(QcTimeExamineCollectVo vo) {
        PageHelper.startPage(vo.getPageDate());
        List<QcTimeExamineReceiveSheet> sheets = qcTimeExamineReceiveSheetMapper.receiveSheet(vo);
        assert sheets != null;
        for (QcTimeExamineReceiveSheet sheet : sheets){
            sheet.setPickupTimelyRate(sheet.getPickupTimelyRate() + "%");
            sheet.setPickupNotTimelyRate(sheet.getPickupNotTimelyRate() + "%");
        }
        return sheets;
    }

    @Override
    public QcTimeExamineCollect collectReceive(QcTimeExamineCollectVo vo) {
        QcTimeExamineCollect collect = qcTimeExamineReceiveCollectMapper.collectReceive(vo);
        collect.setCollectDate(vo.getStartTime() + "-" + vo.getEndTime());
        collect.setAimValues(0.95);
        collect.setCode("receive");
        collect.setSeven(receiveSevens(GetDate.getSevenDate()));
        return collect;
    }

    @Override
    public List<QcTimeExamineSeven> receiveSevens(List<String> date) {
        List<QcTimeExamineSeven> sevens = qcTimeExamineReceiveSevenMapper.receiveSevens(sevenDate);
        List<QcTimeExamineSeven> list = new ArrayList<>();
        for (int i = 0; i < sevenDate.size(); i++) {
            QcTimeExamineSeven seven = new QcTimeExamineSeven();
            seven.setCollectDate(sevenDate.get(i));
            boolean bool = false;
            for (int m = 0; m < sevens.size(); m++) {
                if (sevenDate.get(i).equals(sevens.get(m).getCollectDate())) {
                    seven.setRate(sevens.get(m).getRate());
                    bool = true;
                    break;
                }
            }
            if (!bool) {
                seven.setRate("0");
            }
            list.add(seven);
        }
        return list;
    }

    @Override
    public List<QcTimeExamineStrandedSheet> stranded(QcTimeExamineCollectVo vo) {
        PageHelper.startPage(vo.getPageDate());
        List<QcTimeExamineStrandedSheet> strandedSheets = qcTimeExamineStrandedSheetMapper.stranded(vo);
        assert strandedSheets != null;
        for (QcTimeExamineStrandedSheet strandedSheet : strandedSheets) {
            strandedSheet.setExamineAimValue("无配置");
            strandedSheet.setTotalStrandedRate(strandedSheet.getTotalStrandedRate() + "%");
            strandedSheet.setExamineStrandedRate(strandedSheet.getExamineStrandedRate() + "%");
            strandedSheet.setStagnantTwentyFourRate(strandedSheet.getStagnantTwentyFourRate() + "%");
        }
        return strandedSheets;
    }

    @Override
    public QcTimeExamineCollect collectStranded(QcTimeExamineCollectVo vo) {
        QcTimeExamineCollect collect = qcTimeExamineStrandedCollectMapper.collectStranded(vo);
        collect.setCollectDate(vo.getStartTime() + "-" + vo.getEndTime());
        collect.setAimValues(null);
        collect.setPreExamineAmount(new BigDecimal(0));
        collect.setCode("stranded");
        collect.setSeven(strandedSeven(GetDate.getSevenDate()));
        return collect;
    }

    @Override
    public List<QcTimeExamineSeven> strandedSeven(List<String> date) {
        List<QcTimeExamineSeven> sevens = qcTimeExamineStrandedSevenMapper.strandedSeven(sevenDate);
        List<QcTimeExamineSeven> list = new ArrayList<>();
        for (int i = 0; i < sevenDate.size(); i++) {
            QcTimeExamineSeven seven = new QcTimeExamineSeven();
            seven.setCollectDate(sevenDate.get(i));
            boolean bool = false;
            for (int m = 0; m < sevens.size(); m++) {
                if (sevenDate.get(i).equals(sevens.get(m).getCollectDate())) {
                    seven.setRate(sevens.get(m).getRate());
                    bool = true;
                    break;
                }
            }
            if (!bool) {
                seven.setRate("0");
            }
            list.add(seven);
        }
        return list;
    }

    @Override
    public List<QcTimeExamineReturnGoodSheet> returnGoodSheet(QcTimeExamineCollectVo vo) {
        PageHelper.startPage(vo.getPageDate());
        List<QcTimeExamineReturnGoodSheet> sheets = qcTimeExamineReturnGoodSheetMapper.returnGoodSheet(vo);
        assert sheets != null;
        for (QcTimeExamineReturnGoodSheet sheet : sheets) {
            sheet.setReturnGoodTimelyRate(sheet.getReturnGoodTimelyRate() + "%");
            sheet.setReturnGoodHoldupRate(sheet.getReturnGoodTimelyRate() + "%");
        }
        return sheets;
    }

    @Override
    public QcTimeExamineCollect collectReturnGood(QcTimeExamineCollectVo vo) {
        QcTimeExamineCollect collect = qcTimeExamineReturnGoodCollectMapper.collectReturnGood(vo);
        collect.setCollectDate(vo.getStartTime() + "-" + vo.getEndTime());
        collect.setAimValues(null);
        collect.setCode("returnGood");
        collect.setSeven(returnGoodSeven(GetDate.getSevenDate()));
        return collect;
    }

    @Override
    public List<QcTimeExamineSeven> returnGoodSeven(List<String> date) {
        List<QcTimeExamineSeven> sevens = qcTimeExamineReturnGoodSevenMapper.returnGoodSeven(sevenDate);
        List<QcTimeExamineSeven> list = new ArrayList<>();
        for (int i = 0; i < sevenDate.size(); i++) {
            QcTimeExamineSeven seven = new QcTimeExamineSeven();
            seven.setCollectDate(sevenDate.get(i));
            boolean bool = false;
            for (int m = 0; m < sevens.size(); m++) {
                if (sevenDate.get(i).equals(sevens.get(m).getCollectDate())) {
                    seven.setRate(sevens.get(m).getRate());
                    bool = true;
                    break;
                }
            }
            if (!bool) {
                seven.setRate("0");
            }
            list.add(seven);
        }
        return list;
    }

    @Override
    public List<QcTimeExamineSignSheet> signSheet(QcTimeExamineCollectVo vo) {
        PageHelper.startPage(vo.getPageDate());
        List<QcTimeExamineSignSheet> sheets = qcTimeExamineSignSheetMapper.signSheet(vo);
        assert sheets != null;
        for (QcTimeExamineSignSheet sheet : sheets) {
            sheet.setP1SignRate(sheet.getP1SignRate() + "%");
            sheet.setP2SignRate(sheet.getP2SignRate() + "%");
//            sheet.setToTimelySignRate(sheet.getToTimelySignRate() + "%");
            sheet.setToSignDelay0To24Rate(sheet.getToSignDelay0To24Rate() + "%");
            sheet.setToSignDelay24To48Rate(sheet.getToSignDelay24To48Rate() + "%");
            sheet.setToSignDelayOver48Rate(sheet.getToSignDelayOver48Rate() + "%");
        }
        return sheets;
    }

    @Override
    public QcTimeExamineCollect collectSign(QcTimeExamineCollectVo vo) {
        QcTimeExamineCollect collect = qcTimeExamineSignCollectMapper.collectSign(vo);
            collect.setCollectDate(vo.getStartTime() + "-" + vo.getEndTime());
            collect.setAimValues(0.965);
            collect.setP1aimValues(0.7793);
            collect.setP2aimValues(0.9438);
            collect.setCode("sign");
            collect.setSeven(signSeven(GetDate.getSevenDate()));
        return collect;
    }

    @Override
    public List<QcTimeExamineSeven> signSeven(List<String> date) {
        List<QcTimeExamineSeven> sevens = qcTimeExamineSignSevenMapper.signSeven(sevenDate);
        List<QcTimeExamineSeven> list = new ArrayList<>();
        for (int i = 0; i < sevenDate.size(); i++) {
            QcTimeExamineSeven seven = new QcTimeExamineSeven();
            seven.setCollectDate(sevenDate.get(i));
            boolean bool = false;
            for (int m = 0; m < sevens.size(); m++) {
                if (sevenDate.get(i).equals(sevens.get(m).getCollectDate())) {
                    seven.setRate(sevens.get(m).getRate());
                    bool = true;
                    break;
                }
            }
            if (!bool) {
                seven.setRate("0");
            }
            list.add(seven);
        }
        return list;
    }
}
